
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Produk;
import validation.ValidateForm;
import database.DBClass;
import java.util.List;

/**
 *
 * @author Raven
 */
public class ControllerServlet extends HttpServlet {

    //declare values to get form values from jsp page
    String idproduk;
    String namaproduk;
    String hargaproduk;
    String jenisproduk;
    String deskripsiproduk;
    String idkopf;
    String action;
    Produk produk = new Produk();
    ValidateForm validateform = new ValidateForm();
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            idproduk = request.getParameter("idproduk");
            namaproduk = request.getParameter("namaproduk");
            hargaproduk = request.getParameter("hargaproduk");
            jenisproduk = request.getParameter("jenisproduk");
            deskripsiproduk = request.getParameter("deskripsiproduk");
            idkopf = request.getParameter("idkopf");
            action = request.getParameter("action");
            if (action.equals("Insert Produk")) {
                //set values to produk.For this call below method
                setValuesToproduk();

                //check all form values are valid or not. send produk object 
                Produk checkedproduk = validateform.validateData(produk);
                if (!checkedproduk.getIsValid()) {

                    //if data is invalid.set produk object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedproduk.setAction("Insert Produk");
                    request.setAttribute("error", checkedproduk);

                    RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdate.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass produk object
                    dbobject.insertProduk(produk);
                    List list = dbobject.getAllproduk();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("admin/view.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }
            }
            if (action.equals("editproduk")) {
                //get userdetails of particular name
                Produk uproduk = dbobject.getProduk(idproduk);
                System.out.println(produk.getnamaproduk());
                request.setAttribute("updateuser", uproduk);
                RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdate.jsp");
                rd.forward(request, response);
            }
            if (action.equals("Update Produk")) {


                setValuesToproduk();
                Produk checkedproduk = validateform.validateData(produk);
                System.out.println(idproduk);
                if (!checkedproduk.getIsValid()) {

                    //if data is invalid.set produk object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedproduk.setAction("Update Produk");
                    request.setAttribute("error", checkedproduk);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/insertupdate.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass produk object
                    
                    dbobject.UpdateProduk(produk, idproduk);
                    
                    List list = dbobject.getAllproduk();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("admin/view.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }

            }
            if (action.equals("deleteproduk")) {
                //delete userdetails of particular name
                dbobject.deleteproduk(idproduk);
                List list = dbobject.getAllproduk();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("admin/view.jsp");
                rd.forward(request, response);
            }
            if (action.equals("detailproduk")) {
                request.setAttribute("linkkoperasi",idproduk);
                 Produk uproduk = dbobject.getProduk(idproduk);
                System.out.println(produk.getnamaproduk());
                request.setAttribute("detailproduk", uproduk);
                RequestDispatcher rd = request.getRequestDispatcher("produkdetails.jsp");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            out.println(e);
            System.out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to produk

    public void setValuesToproduk() {
        produk.setnamaproduk(namaproduk);
        produk.sethargaproduk(hargaproduk);
        produk.setjenisproduk(jenisproduk);
        produk.setdeskripsiproduk(deskripsiproduk);
        produk.setidkopf(idkopf);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}
